/*
 *   Copyright 2023 <a href="mailto:asialjim@hotmail.com">Asial Jim</a>
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.asialjim.encommunicate.rsa.management.key;

import com.asialjim.encommunicate.rsa.management.pojo.AppEnDecryptInfo;
import jakarta.validation.constraints.NotNull;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 基于内存的 RSA 密钥对数据仓库
 *
 * @author Copyright © <a href="mailto:asialjim@hotmail.com">Asial Jim</a>   Co., LTD
 * @version 1.0
 * @since 2023/2/27, &nbsp;&nbsp; <em>version:1.0</em>, &nbsp;&nbsp; <em>java version:17</em>
 */
@Component
public class RSAKeyMemoryRepository implements RSAKeyRepository {
    private static final Map<String, AppEnDecryptInfo> EN_DECRYPT_INFO_MAP = new ConcurrentHashMap<>();

    @Override
    public AppEnDecryptInfo modify(@NotNull AppEnDecryptInfo info) {
        EN_DECRYPT_INFO_MAP.put(info.getAppid(), info);
        return info;
    }

    @Override
    public Set<AppEnDecryptInfo> infos() {
        return new HashSet<>(EN_DECRYPT_INFO_MAP.values());
    }

    @Override
    public AppEnDecryptInfo enDecryptInfo(String appid) {
        return EN_DECRYPT_INFO_MAP.get(appid);
    }
}